<script lang="ts" setup>
import { useRouter } from "vue-router";
import { onMounted, ref } from "vue";
import type { UserType } from "../models/user";
import { getCurrentUser, isUserLoggedIn } from "../service/user.ts";
import { showFailToast, showSuccessToast, showConfirmDialog } from "vant/es";

const router = useRouter();
const currentUser = ref<UserType | null>(null);
const isLoading = ref(true);
const loadError = ref(false);

onMounted(async () => {
  try {
    isLoading.value = true;
    loadError.value = false;
    
    const user = await getCurrentUser();
    
    if (user) {
      currentUser.value = user;
      showSuccessToast("获取用户信息成功");
    } else {
      loadError.value = true;
      // 不直接跳转，让用户选择
      showFailToast("获取用户信息失败，请检查登录状态");
    }
  } catch (error) {
    console.error("获取用户信息失败:", error);
    loadError.value = true;
    showFailToast("获取用户信息失败，请重试");
  } finally {
    isLoading.value = false;
  }
});

const toEdit = (editKey: string, currentValue: string | number, editName: string) => {
  router.push({
    path: "/user/edit",
    query: {
      editKey,
      currentValue,
      editName,
    }
  });
};

const retryLoad = async () => {
  try {
    isLoading.value = true;
    loadError.value = false;
    
    const user = await getCurrentUser();
    
    if (user) {
      currentUser.value = user;
      showSuccessToast("获取用户信息成功");
    } else {
      loadError.value = true;
      showFailToast("获取用户信息失败，请检查登录状态");
    }
  } catch (error) {
    console.error("获取用户信息失败:", error);
    loadError.value = true;
    showFailToast("获取用户信息失败，请重试");
  } finally {
    isLoading.value = false;
  }
};

// 跳转到登录页面
const goToLogin = async () => {
  const result = await showConfirmDialog({
    title: '需要登录',
    message: '您需要先登录才能访问此页面，是否前往登录？',
    confirmButtonText: '去登录',
    cancelButtonText: '取消'
  });
  
  if (result) {
    router.push("/login");
  }
};
</script>

<template>
  <div class="user-page">
    <!-- 加载状态 -->
    <div v-if="isLoading" class="loading-container">
      <van-loading size="24px" vertical>正在加载用户信息...</van-loading>
    </div>
    
    <!-- 错误状态 -->
    <div v-else-if="loadError" class="error-container">
      <van-empty 
        image="error" 
        description="加载用户信息失败"
        class="error-state"
      >
        <div class="error-actions">
          <van-button type="primary" @click="retryLoad" class="retry-btn">
            <van-icon name="replay" size="16" />
            重新加载
          </van-button>
          <van-button type="default" @click="goToLogin" class="login-btn">
            <van-icon name="user-o" size="16" />
            去登录
          </van-button>
        </div>
      </van-empty>
    </div>
    
    <!-- 用户信息内容 -->
    <div v-else-if="currentUser">
      <!-- 用户信息卡片 -->
      <div class="user-info-card">
        <div class="user-avatar-section">
          <van-image
            :src="currentUser.avatarUrl || '/default-avatar.png'"
            class="user-avatar"
            round
            fit="cover"
          />
          <div class="user-basic-info">
            <h3 class="user-name">{{ currentUser.userName }}</h3>
            <p class="user-account">{{ currentUser.userAccount }}</p>
          </div>
        </div>
      </div>
      
      <!-- 功能菜单 -->
      <div class="menu-container">
        <van-cell-group class="menu-group">
          <van-cell 
            title="个人信息修改" 
            is-link 
            to="/user/update"
            class="menu-item"
            icon="edit"
          >
            <template #right-icon>
              <van-icon name="arrow" />
            </template>
          </van-cell>
          
          <van-cell 
            title="创建的队伍" 
            is-link 
            to="/team/create"
            class="menu-item"
            icon="friends-o"
          >
            <template #right-icon>
              <van-icon name="arrow" />
            </template>
          </van-cell>
          
          <van-cell 
            title="加入的队伍" 
            is-link 
            to="/team/join"
            class="menu-item"
            icon="user-o"
          >
            <template #right-icon>
              <van-icon name="arrow" />
            </template>
          </van-cell>
        </van-cell-group>
      </div>
    </div>
    
    <!-- 未登录状态 -->
    <div v-else class="not-logged-container">
      <van-empty 
        image="user-o" 
        description="请先登录"
        class="not-logged-state"
      >
        <van-button type="primary" @click="goToLogin">
          <van-icon name="user-o" size="16" />
          去登录
        </van-button>
      </van-empty>
    </div>
  </div>
</template>



<style scoped>
.user-page {
  min-height: 100vh;
  background: transparent;
  padding: 16px;
}

.user-info-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  padding: 24px;
  margin-bottom: 20px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  animation: fadeInUp 0.6s ease-out;
}

.user-avatar-section {
  display: flex;
  align-items: center;
  gap: 16px;
}

.user-avatar {
  width: 80px;
  height: 80px;
  border: 4px solid rgba(102, 126, 234, 0.2);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.user-basic-info {
  flex: 1;
}

.user-name {
  font-size: 24px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0 0 8px 0;
}

.user-account {
  font-size: 16px;
  color: #7f8c8d;
  margin: 0;
}

.menu-container {
  animation: fadeInUp 0.8s ease-out;
}

.menu-group {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  overflow: hidden;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.menu-item {
  transition: all 0.3s ease;
  border-bottom: 1px solid rgba(102, 126, 234, 0.1);
}

.menu-item:last-child {
  border-bottom: none;
}

.menu-item:hover {
  background: rgba(102, 126, 234, 0.05);
  transform: translateX(4px);
}

.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 50vh;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  margin: 16px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.error-container {
  margin: 16px;
}

.error-state {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  padding: 40px 20px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.not-logged-container {
  margin: 16px;
}

.not-logged-state {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  padding: 40px 20px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.error-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-top: 16px;
}

.retry-btn {
  flex: 1;
  max-width: 120px;
}

.login-btn {
  flex: 1;
  max-width: 120px;
  background: rgba(102, 126, 234, 0.1);
  color: #667eea;
  border: 1px solid #667eea;
  border-radius: 20px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;
}

.login-btn:hover {
  background: #667eea;
  color: white;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

/* 按钮样式优化 */
:deep(.van-button--primary) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 20px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;
}

:deep(.van-button--primary:hover) {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

:deep(.van-button--primary:active) {
  transform: translateY(0);
}

/* 深度选择器优化 Vant 组件样式 */
:deep(.van-cell__title) {
  font-weight: 500;
  color: #2c3e50;
  font-size: 16px;
}

:deep(.van-cell__value) {
  color: #7f8c8d;
}

:deep(.van-cell__right-icon) {
  color: #bdc3c7;
  transition: color 0.3s ease;
}

:deep(.van-cell:hover .van-cell__right-icon) {
  color: #667eea;
}

:deep(.van-image__img) {
  border-radius: 50%;
}

:deep(.van-loading__text) {
  color: #667eea;
  font-weight: 500;
  margin-top: 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .user-page {
    padding: 12px;
  }
  
  .user-info-card {
    padding: 20px;
    margin-bottom: 16px;
  }
  
  .user-avatar {
    width: 70px;
    height: 70px;
  }
  
  .user-name {
    font-size: 20px;
  }
  
  .user-account {
    font-size: 14px;
  }
  
  .menu-item:hover {
    transform: none;
  }
}
</style>